package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.logic.ResultSetParseEntityManger;
import com.yanqu.road.entity.orewar.OrePitInfo;
import com.yanqu.road.entity.orewar.UserOrePit;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.player.UserPatronsSkill;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserOreWarDaoImpl extends TempDao {

    public Map<Integer, UserOrePit> getUserOrePitMap(int activityId) {
        Map<Integer, UserOrePit> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_orewar t1 " +
                "LEFT JOIN t_u_patrons t2 ON t1.userId = t2.userId AND INSTR(t1.patronsIdList,t2.patronsId) " +
                "LEFT JOIN t_u_patrons_skill t3 ON t1.userId = t3.userId AND t2.patronsId = t3.patronsId " +
                "WHERE t1.activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserOrePit data = initUserOrePit(rs);
                dataMap.put(data.getPitId(), data);

                if(rs.getInt("patronsId") > 0){
                    data = dataMap.get(data.getPitId());
                    UserPatrons userPatrons = ResultSetParseEntityManger.initUserPatrons(rs);
                    if(!data.isContainsPatrons(userPatrons.getPatronsId())){
                        data.addUserPatrons(userPatrons);
                    }
                    if(rs.getInt("skillId") > 0) {
                        userPatrons = data.getUserPatrons(userPatrons.getPatronsId());
                        UserPatronsSkill userPatronsSkill = ResultSetParseEntityManger.initUserPatronsSkill(rs);
                        if(!userPatrons.isContainsSkill(userPatronsSkill.getType(), userPatronsSkill.getSkillId())){
                            userPatrons.addSkill(userPatronsSkill);
                        }
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public boolean initUserOrePitMap(Map<Integer, OrePitInfo> minePitInfoMap, Map<Integer, UserOrePit> userOrePitMap) {
        boolean result = false;
        String sql = "INSERT INTO t_u_activity_orewar(activityId,pitId,stationId,userId,patronsIdList,joinTime,lastGetRewardTime,nickName,decoration,unionName,isBlowout)"
                + " VALUES(?,?,?,?,?,?,?,?,?,?,?)";
        getDb().setConnName(DbNameStrategy.getUserDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (OrePitInfo minePit : minePitInfoMap.values()) {
            if (!userOrePitMap.containsKey(minePit.getPitId())) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.INTEGER, minePit.getActivityId()));
                para.put(i++, new DbParameter(Types.INTEGER, minePit.getPitId()));
                para.put(i++, new DbParameter(Types.INTEGER, minePit.getStationId()));
                para.put(i++, new DbParameter(Types.BIGINT, 0L));
                para.put(i++, new DbParameter(Types.VARCHAR, ""));
                para.put(i++, new DbParameter(Types.BIGINT, 0L));
                para.put(i++, new DbParameter(Types.BIGINT, 0L));
                para.put(i++, new DbParameter(Types.VARCHAR, ""));
                para.put(i++, new DbParameter(Types.VARCHAR, ""));
                para.put(i++, new DbParameter(Types.VARCHAR, ""));
                para.put(i++, new DbParameter(Types.BOOLEAN, false));
                mapList.add(para);
            }
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateUserOrePitList(List<UserOrePit> userOrePitList) {
        boolean result = false;
        String sql = "UPDATE t_u_activity_orewar SET stationId=?,userId=?,patronsIdList=?,joinTime=?,lastGetRewardTime=?,nickName=?,decoration=?,unionName=?,isBlowout=? WHERE "
                + "activityId=? and pitId=?";
        getDb().setConnName(DbNameStrategy.getUserDb());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (UserOrePit orePit : userOrePitList) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, orePit.getStationId()));
            para.put(i++, new DbParameter(Types.BIGINT, orePit.getUserId()));
            para.put(i++, new DbParameter(Types.VARCHAR, StringUtils.listToString(orePit.getPatronsIdList(), ',')));
            para.put(i++, new DbParameter(Types.BIGINT, orePit.getJoinTime()));
            para.put(i++, new DbParameter(Types.BIGINT, orePit.getLastGetRewardTime()));
            para.put(i++, new DbParameter(Types.VARCHAR, orePit.getNickName()));
            para.put(i++, new DbParameter(Types.VARCHAR, orePit.getDecoration()));
            para.put(i++, new DbParameter(Types.VARCHAR, orePit.getUnionName()));
            para.put(i++, new DbParameter(Types.BOOLEAN, orePit.isBlowout()));
            para.put(i++, new DbParameter(Types.INTEGER, orePit.getActivityId()));
            para.put(i++, new DbParameter(Types.INTEGER, orePit.getPitId()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    private UserOrePit initUserOrePit(ResultSet rs) throws SQLException {
        UserOrePit data = new UserOrePit();
        data.setActivityId(rs.getInt("activityId"));
        data.setPitId(rs.getInt("pitId"));
        data.setStationId(rs.getInt("stationId"));
        data.setUserId(rs.getLong("userId"));
        data.setPatronsIdList(StringUtils.stringToIntegerListNoZero(rs.getString("patronsIdList"), ","));
        data.setJoinTime(rs.getLong("joinTime"));
        data.setLastGetRewardTime(rs.getLong("lastGetRewardTime"));
        data.setNickName(rs.getString("nickName"));
        data.setDecoration(rs.getString("decoration"));
        data.setUnionName(rs.getString("unionName"));
        data.setBlowout(rs.getBoolean("isBlowout"));
        return data;
    }
}
